package com.dormitory.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.dormitory.R;
import com.dormitory.application.MyApplication;
import com.dormitory.bean.UserBean;
import com.dormitory.controller.DormitoryController;

public class AddNotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_notification);

        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_notification_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_confirm:
                String title = ((TextView) findViewById(R.id.textView3)).getText().toString();
                String content = ((TextView)findViewById(R.id.textView6)).getText().toString();
                String ausername=((MyApplication)getApplication()).getUser().getName();
                int dormitoryId = ((MyApplication)getApplication()).getDormitory().getId();
                AddNotificationAsyncTask addnotificationAsyncTask = new AddNotificationAsyncTask();
                addnotificationAsyncTask.execute(title, content,dormitoryId,ausername);
                return true;
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    /*private class UploadAsyncTask extends AsyncTask<Object, Object, Boolean> {

        private UserBean user;
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(FileActivity.this);
            progressDialog.setTitle("上传中");
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected Boolean doInBackground(Object[] params) {
            FileController fileController = FileController.getInstance();
            return fileController.uploadFile((File)params[0], (String)params[1], (Integer)params[2], (Boolean)params[3]);
        }

        @Override
        protected void onPostExecute(Boolean code) {
            super.onPostExecute(code);
            if(progressDialog != null && progressDialog.isShowing()) {
                progressDialog.cancel();
            }
            DormitoryBean dormitoryBean = ((MyApplication)getApplication()).getDormitory();
            if(dormitoryBean != null) {
                GetFileListAsyncTask getFileListAsyncTask = new GetFileListAsyncTask();
                getFileListAsyncTask.execute(dormitoryBean.getId());
            }
            if(code) {
                Toast.makeText(FileActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(FileActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
            }
        }
    }*/

    private class AddNotificationAsyncTask extends AsyncTask<Object, Object, Integer> {

        private UserBean user;
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(AddNotificationActivity.this);
            progressDialog.setTitle("上传中");
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
        @Override
        protected Integer doInBackground(Object[] params) {
            user = new UserBean();
            DormitoryController dormitoryController = DormitoryController.getInstance();
            return dormitoryController.addnotification((String)params[0], (String) params[1], (Integer)params[2],(String) params[3]);
        }

        @Override
        protected void onPostExecute(Integer code) {
            super.onPostExecute(code);
            if(progressDialog != null && progressDialog.isShowing()) {
                progressDialog.cancel();
            }
            switch(code) {
                case 0:
                    Handler handler = ((MyApplication)getApplication()).getUpdateNotiHandler();
                    if(handler != null) {
                        handler.sendEmptyMessage(0x101);
                    }
                    finish();
                    break;
                default:
                    Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
